import ObjUtil from "./ObjUtil";
import StrUtil from "./StrUtil";

class CodeGen {
  static getObjName(objName) {
    objName = objName.trim()
    if (objName.endsWith("s")) {
      // console.log("objName.length");
      // console.log(objName.length);
      return objName.substr(0, objName.length - 1)
    }
    return objName
  }

  static getEntityName(objName) {
    objName = objName.trim()
    // if (objName.endsWith("Dtos")) {
    //   // console.log("objName.length");
    //   // console.log(objName.length);
    //   return objName.substr(0, objName.length - 4)
    // }
    return StrUtil.backDelStr(objName, "Dto")
    // return StrUtil.backDelStr(objName, "Dtos")
    // return objName
  }

  static getForCodeFrontDto(listName, parentObjName) {
    // let listName = key
    let objName = this.getObjName(listName)
    console.log("objName 他是dto吗");
    console.log(objName);
    // let isDto = objName.endsWith("Dtos")
    let isDto = objName.endsWith("Dto")
    // console.log("isDto");
    // console.log(isDto);
    // if(objName.endsWith("Dtos")){

    // }
    let entityName = this.getEntityName(objName)
    // let objName = this.getObjName(listName)
    // objName
    // console.log("parentObjName");
    // console.log(parentObjName);
    let parentClassName = ""
    try {
      parentClassName = StrUtil.upperCaseFirst(parentObjName)
    } catch (e) {
      console.log(e);
      console.log("错误的 upoer parentObjName  " + parentObjName);
    }

    // console.log("objName");
    // console.log(objName);
    let objId = objName + "Id"
    // console.log("objId");
    // console.log(objId);
    let objClassName = StrUtil.upperCaseFirst(objName)
    // console.log("objClassName");
    // console.log(objClassName);
    let linkClassName = `${parentClassName}${objClassName}`
    let linkObjName = `${parentObjName}${objClassName}`
    let RepositoryName = objName + "Repository"
    //  ${childForCode}
    // ${parentId}
    let toEntityCode = ` ${objClassName} ${entityName} = ${objName}.toEntity();  `
    let forCode = `\n  for (${objClassName} ${objName} : ${listName} ) {
        ${isDto?toEntityCode:""}
    ${objClassName} saved${objClassName}=  ${RepositoryName}.save( ${objName} );
    Integer ${objId} = saved${objClassName}.getId();
     ${linkClassName} ${linkObjName} =new   ${linkClassName}();
     ${linkClassName}.set${objClassName}Id(${objName}Id);
    //  父亲的id 上面会产生的
     ${linkClassName}.set${parentClassName}Id(${parentObjName}Id);
     `

    return forCode
  }


  static getForCodeFront(listName, parentObjName) {
    let  parentDtoName= parentObjName
    parentObjName=this.getEntityName(parentObjName)
    // let listName = key
    let objName = this.getObjName(listName)
    console.log("objName 他是dto吗");
    console.log(objName);
    // let isDto = objName.endsWith("Dtos")
    let isDto = objName.endsWith("Dto")
    // console.log("isDto");
    // console.log(isDto);
    // if(objName.endsWith("Dtos")){

    // }
    let entityName = this.getEntityName(objName)
    let upEntityName = StrUtil.upperCaseFirst(entityName)
    // let objName = this.getObjName(listName)
    // objName
    // console.log("parentObjName");
    // console.log(parentObjName);
    let parentClassName = ""
    try {
      parentClassName = StrUtil.upperCaseFirst(parentObjName)
    } catch (e) {
      console.log(e);
      console.log("错误的 upoer parentObjName  " + parentObjName);
    }

    // console.log("objName");
    // console.log(objName);
    let objId = objName + "Id"
    // console.log("objId");
    // console.log(objId);
    let objClassName = StrUtil.upperCaseFirst(objName)
    // console.log("objClassName");
    // console.log(objClassName);
    // let linkClassName = `${parentClassName}${objClassName}`
    // let linkObjName = `${parentObjName}${objClassName}`

    let linkClassName = `${parentClassName}${upEntityName}`
    let linkObjName = `${parentObjName}${upEntityName}`
    // let RepositoryName = objName + "Repository"
    // let RepositoryName = upEntityName + "Repository"
    let RepositoryName = entityName + "Repository"
    //  ${childForCode}
    // ${parentId}
    let toEntityCode = ` ${upEntityName} ${entityName} = ${objName}.toEntity();  `
    let forCode = `\n 
    
    List<${objClassName}>  ${listName}  = ${parentDtoName}.get${objClassName}s();
     for (${objClassName} ${objName} : ${listName} ) {
        ${isDto?toEntityCode:""}
    ${upEntityName} saved${upEntityName}=  ${RepositoryName}.save( ${entityName} );
    Integer ${entityName}Id = saved${upEntityName}.getId();
     ${linkClassName} ${linkObjName} =new   ${linkClassName}();
     ${linkObjName}.set${upEntityName}Id(${entityName}Id);
    //  父亲的id 上面会产生的
     ${linkObjName}.set${parentClassName}Id(${parentObjName}Id);
     `

    return forCode
  }


  static getForCode(key) {
    let listName = key
    let objName = this.getObjName(key)
    console.log("objName");
    console.log(objName);
    let objId = objName + "Id"
    console.log("objId");
    console.log(objId);
    let objClassName = StrUtil.upperCaseFirst(objName)
    console.log("objClassName");
    console.log(objClassName);
    let RepositoryName = objName + "Repository"
    //  ${childForCode}
    let forCode = `for (${objClassName} ${objName} : ${listName} ) {
    ${objClassName} save=  ${RepositoryName}.save( ${objName} );
    Integer ${objId} = save.getId();
  }`
    return forCode
  }

  static genJpaSaveRec2(jsons, name, sign) {
    if (name == "" || name == undefined) {
      name = "json"
    }
    for (let key in jsons) {
      var newName = name + sign + key;
      let val = jsons[key]
      let notObj = !(val instanceof Object)
      if (notObj) {
        console.log(newName + " = " + val); //如果不是Object则打印键值
      } else {
        ObjUtil.getAllJson(val, newName, sign); //如果是Object则递归
      }
    }
  };

  static genJpaSaveRec(json, parentObjName) {
    // formName = formName || "form"
    let formRows = "";
    // console.log("postFormPlan");
    // console.log(json);
    let childrenForCodeLst = []
    let haveArr = false
    // tree.children = []
    let forCode = ""
    // console.log("parentObjName");
    // console.log(parentObjName);
    for (let fieldName in json) {
      // console.log(o);
      let key = fieldName;
      let fieldVal = json[fieldName];
      // console.log(val);
      // <el-form-item>${key} </el-form-item>
      // let typeofO=   typeof o
      // console.log("typeofO");
      // console.log(typeofO);
      // let typeofVal=   typeof val

      let isArr = ObjUtil.isArr(fieldVal)

      if (isArr) {
        haveArr = true
        // console.log("is arr");
        // console.log(key);
        // ke 
        // if(key.endsWith(",")){

        // }

        let listName = key
        let objName = this.getObjName(listName)
        // console.log("内部");
        // console.log("objName");
        // console.log(objName);
        // let objId = objName + "Id"
        // console.log("parentObjName");
        // console.log(parentObjName);
        // parentObjName=objName
        forCode += CodeGen.getForCodeFront(listName, parentObjName) + "\n"
        let firstVal = fieldVal[0]
        // nowCode+=forCode+"\n"
        // tree.children.push(forCode)
        // let childForCode = CodeGen.genJpaSaveRec(firstVal,objId)
        let childForCode = CodeGen.genJpaSaveRec(firstVal, objName)
        forCode += childForCode + "\n"
        forCode += "   }\n"
        // tree.children.push(childForCode)
        // nowCode+=childForCode+"\n"
        // // 递归获取 list的 东西
        // childrenForCodeLst.push(childForCode)


        // for(let ii in val){
        //   let valii=  val[ii]
        //   console.log("ii");
        //   console.log(ii);
        //   console.log("valii");
        //   console.log(valii);
        // }

        // let forCode = CodeGen.getForCode(key)
        // nowCode+=forCode+"\n"

        // StrUtil. 
        //   let listName = key
        //   let objName = this.getObjName(key)
        //   console.log("objName");
        //   console.log(objName);
        //   let objId = objName + "Id"
        //   console.log("objId");
        //   console.log(objId);
        //   let objClassName = StrUtil.upperCaseFirst(objName)
        //   console.log("objClassName");
        //   console.log(objClassName);
        //   let RepositoryName = objName + "Repository"
        //   let forCode = `for (${objClassName} ${objName} : ${listName} ) {
        //   ${objClassName} save=  ${RepositoryName}.save( ${objName} );
        //   Integer ${objId} = save.getId();
        //    // 他是father id 
        //    ${childForCode}

        // }`

        // console.log("forCode");
        // console.log(forCode);
        // return nowCode
      }



      // if(haveArr==false){
      //   return nowCode
      // }



      // console.log("typeofVal");
      // console.log(typeofVal);
    }
    return forCode
    //   let formVueStr = `<el-form>
    //     ${formRows}
    // </el-form>`;
    //   console.log(formVueStr);

  }

  static genJpaSave(json, parentObjName) {
    console.log(json);

    // js 获取 obj的名字

    let forCode = ""
    for (let fieldName in json) {
      let key = fieldName;
      let fieldVal = json[fieldName];
      // console.log(fieldName);

      let isArr = ObjUtil.isArr(fieldVal)

      if (isArr) {
        // haveArr = true
        let listName = key
        let objName = this.getObjName(listName)
        // console.log("objName");
        // console.log(objName);
        let objId = objName + "Id"
        // console.log("parentObjName");
        // console.log(parentObjName);
        // let parentObjName=json.name
        // let parentObjName=fieldName
        forCode += CodeGen.getForCodeFront(listName, parentObjName) + "\n"
        let firstVal = fieldVal[0]

        let childForCode = CodeGen.genJpaSaveRec(firstVal, parentObjName)
        forCode += childForCode + "\n"
        forCode += "   }\n"

      }

    }
    console.log(forCode);
    return forCode

    // let code = CodeGen.genJpaSaveRec(postFormPlan, formName)
    // console.log("code");
    // console.log(code);
    //   formName = formName || "form"
    //   let formRows = "";
    //   for (let o in postFormPlan) {
    //     // console.log(o);
    //     let key = o;
    //     let val = postFormPlan[o];
    //     // console.log(val);
    //     // <el-form-item>${key} </el-form-item>
    //     // let typeofO=   typeof o
    //     // console.log("typeofO");
    //     // console.log(typeofO);
    //     // let typeofVal=   typeof val

    //     let isArr = ObjUtil.isArr(val)
    //     if (isArr) {
    //       console.log("is arr");
    //       console.log(key);
    //       // ke 
    //       // if(key.endsWith(",")){

    //       // }

    //       for(let ii in val){
    //         let valii=  val[ii]
    //         console.log("ii");
    //         console.log(ii);
    //         console.log("valii");
    //         console.log(valii);
    //       }

    //       // StrUtil. 
    //       let listName = key
    //       let objName = this.getObjName(key)
    //       console.log("objName");
    //       console.log(objName);
    //       let objId = objName + "Id"
    //       console.log("objId");
    //       console.log(objId);
    //       let objClassName =StrUtil. upperCaseFirst(objName)
    //       console.log("objClassName");
    //       console.log(objClassName);
    //       let RepositoryName = objName + "Repository"
    //       let forCode = `for (${objClassName} ${objName} : ${listName} ) {
    //       ${objClassName} save=  ${RepositoryName}.save( ${objName} );
    //       Integer ${objId} = save.getId();
    //        // 他是father id 
    //     }`

    //     console.log("forCode");
    //     console.log(forCode);
    //     }



    //     // console.log("typeofVal");
    //     // console.log(typeofVal);
    //   }
    //   let formVueStr = `<el-form>
    //     ${formRows}
    // </el-form>`;
    //   console.log(formVueStr);
    //   return formVueStr
  }


  static genForm(postFormPlan, formName) {
    formName = formName || "form"
    let formRows = "";
    for (let o in postFormPlan) {
      console.log(o);
      let key = o;
      let val = postFormPlan[o];
      console.log(val);
      // <el-form-item>${key} </el-form-item>

      formRows += `
            <el-form-item label="${key}">
          <el-input  placeholder="${key}" v-model="${formName}.${key}"></el-input>
        </el-form-item>\n`;
    }
    let formVueStr = `<el-form>
      ${formRows}
  </el-form>`;
    console.log(formVueStr);
    return formVueStr
  }

  static genInput(form) {
    let formRows = "";
    for (let o in form) {
      console.log(o);
      let key = o;
      let val = form[o];
      console.log(val);
      // <el-form-item>${key} </el-form-item>
      formRows += `
            <el-form-item label="${key}">
          <el-input v-model="form.${key}"></el-input>
        </el-form-item>\n`;
    }
    let formVueStr = `<el-form>
      ${formRows}
  </el-form>`;
    console.log(formVueStr);
    return formVueStr
  }
}


export default CodeGen
